package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.ProductionPlan;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.ProductionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class ProductionPlanController {
    @Autowired
    ProductionPlanService productionPlanService;

    //查询生产计划
    @GetMapping("/ProductionPlan")
    @ResponseBody
    public ResponseResult<List<ProductionPlan>> getProductionPlans(ProductionPlan productionPlan, int page, int limit){
        ResponseResult<List<ProductionPlan>> responseResult=productionPlanService.getProductionPlans(productionPlan,page,limit);
        System.out.println(responseResult);
        return responseResult;
    }
    //新增计划
    @PostMapping("/ProductionPlan")
    @ResponseBody
    public ResponseResult addProductionPlan(ProductionPlan productionPlan, HttpServletRequest request){
        System.out.println(productionPlan.getPrpId());
        ResponseResult responseResult = null;
        //数据有效性检验
        StringBuilder str=new StringBuilder("");
        if (productionPlan.getPlandate()==null){
            str.append("时间不能为空，");
        }
        if (productionPlan.getPlanperiod()==0){
            str.append("天数不能为0");

        }else {
            //新增计划
            try {
                return productionPlanService.addProductionPlan(productionPlan,request);
            }catch (BusinessException e){
                return new ResponseResult(1,e.getMessage(),null,null);
            }

        }
        return new ResponseResult(1,str.toString(),null,null);
    }

    @PostMapping("deleteProductionPlan")
    @ResponseBody
    public ResponseResult deleteProductionPlan(String prpId){
        return productionPlanService.deleteProductionPlan(prpId);
    }
    @PostMapping("/insertState")
    @ResponseBody
    public ResponseResult insertState(String prpId){
        return productionPlanService.insertState(prpId);
    }

    @GetMapping("/getproduct")
    @ResponseBody
    public ResponseResult<Map> getProductionPlan(String prpId){
        return productionPlanService.getProductionPlan(prpId);
    }
    //修改计划
    @PostMapping("/updateProductionplan")
    @ResponseBody
    public ResponseResult updateProductionplan(ProductionPlan productionPlan){
        System.out.println(productionPlan);
        return productionPlanService.updateProductionplan(productionPlan);
    }
}
